package com.bwie.es.sync;

import com.bwie.common.domain.Shop;
import com.bwie.common.result.Result;
import com.bwie.es.feign.EsFeifn;
import com.bwie.es.service.EsService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author：作者姓名
 * @Package：com.bwie.es.sync
 * @Project：_yk
 * @name：EsSync
 * @Date：2024/6/17 12:57
 */
@Component
@Log4j2
public class EsSync {

    @Autowired
    private EsService esService;

    @Autowired
    private EsFeifn esFeifn;

    @Scheduled(cron = "0/30 * * * * *")
    public void setEsFeifn() {
        Result<List<Shop>> listResult = esFeifn.FindAll();
        List<Shop> shops = listResult.getData();
            shops.forEach(shop -> {
                log.info("同步的数据{}",shop);
            });
            log.info("查询到{}条数据，开始同步……",shops.size());
        if(!CollectionUtils.isEmpty(shops)){
            long l = System.currentTimeMillis();
            //esService.Del();
            esService.Add(shops);
            log.info("同步成功，同步了{}条数据，用时{}毫秒",shops.size(),System.currentTimeMillis()-l);
        }




    }
}
